<template>
<div class="app-container">
  <div style="width: 600px">
    <el-form ref="form" :rules="rules" :model="form" label-width="120px">
      <el-form-item label="标题" prop="title">
        <el-input size="small" v-model="form.title" placeholder="请输入标题" />
      </el-form-item>
      <el-form-item label="轮播图" required>
        <el-upload
          :class="uploadUrl!=''?'hiddenUpload':''"
          action="http://service.dlfpa.org.cn/file/upload"
          list-type="picture-card"
          :on-success="handleSuccess"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </el-form-item>
      <el-form-item label="链接地址" prop="link">
        <el-input size="small" v-model="form.link" placeholder="请输入链接地址" />
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" @click="onSubmit('form')">保存</el-button>
        <el-button size="small" @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        title: '',
        phone: '',
        account: '',
        password: '',
      },
      rules: {
        title: [{
          required: true,
          message: '请输入标题',
          trigger: 'blur'
        }, ],
        link: [{
          required: true,
          message: '请输入链接地址',
          trigger: 'blur'
        }, ],
      },
      dialogImageUrl: '',
      dialogVisible: false,
      uploadStatus: false,
      uploadUrl: '',
    }
  },
  methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList);
      this.uploadUrl = ''
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleSuccess(e) {
      console.log(e)
      if(e.code == 200) {
        this.uploadStatus = true
        this.uploadUrl = e.data.url
      }
    },
    onSubmit(formName) {
      // this.$message('submit!')
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    onCancel() {
      this.$message({
        message: 'cancel!',
        type: 'warning'
      })
    }
  }
}
</script>
<style>
  .hiddenUpload .el-upload--picture-card{
    display:none !important;
  }
</style>
<style scoped>
.line {
  text-align: center;
}
</style>
